package com.learning.platform.controller;

import com.learning.platform.dto.ApiResponse;
import com.learning.platform.service.FileUploadService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/api/v1/upload")
public class UploadController {
    
    private final FileUploadService fileUploadService;
    
    public UploadController(FileUploadService fileUploadService) {
        this.fileUploadService = fileUploadService;
    }
    
    /**
     * 上传课程封面
     */
    @PostMapping("/cover")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<Map<String, String>> uploadCover(@RequestParam("file") MultipartFile file) {
        String coverUrl = fileUploadService.uploadCover(file);
        return ApiResponse.success(Map.of("coverUrl", coverUrl), "封面上传成功");
    }
    
    /**
     * 上传用户头像
     */
    @PostMapping("/avatar")
    @PreAuthorize("hasAnyRole('USER', 'TEACHER', 'ADMIN', 'STUDENT')")
    public ApiResponse<Map<String, String>> uploadAvatar(@RequestParam("file") MultipartFile file) {
        String avatarUrl = fileUploadService.uploadAvatar(file);
        return ApiResponse.success(Map.of("avatarUrl", avatarUrl), "头像上传成功");
    }
    /**
     * 上传课程内容
     */
    @PostMapping("/lesson-content")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<Map<String, String>> uploadLessonContent(@RequestParam("file") MultipartFile file) {
        String contentUrl = fileUploadService.uploadLessonContent(file);
        return ApiResponse.success(Map.of("contentUrl", contentUrl), "内容上传成功");
    }
}










